import { Category } from 'server/models/Category'
import { successResponse, errorResponse } from 'server/utils/response'

export default defineEventHandler(async (event) => {
  try {
    const categories = await Category.getAllCategories()
    
    // 转换为前端需要的格式
    const formattedCategories = categories.map(category => ({
      id: category.id?.toString() || '',
      name: category.name,
      slug: category.slug,
      description: category.description || '',
      color: category.color || '#1890ff',
      icon: category.icon || 'fa:folder',
      postCount: category.post_count || 0,
      status: category.status,
      createdAt: category.created_at ? new Date(category.created_at).toISOString().split('T')[0] : '',
      updatedAt: category.updated_at ? new Date(category.updated_at).toISOString().split('T')[0] : ''
    }))

    return successResponse(formattedCategories, '获取分类列表成功')
  } catch (error) {
    console.error('获取分类列表失败:', error)
    return errorResponse('获取分类列表失败', error instanceof Error ? error.message : '未知错误')
  }
})